﻿using System;
using System.Collections.Generic;
using System.Text;
using LearnNet.Common;
using System.Windows.Forms;
using System.Threading;
namespace Advertise
{
    public class RepostBaidu : IRepost
    {
                private System.Windows.Forms.WebBrowser webBrowser1 = null;
        private LearnNet.Common.XmlHelper xmlHelper = null;
        private static string code = "Baidu";
        public RepostBaidu(System.Windows.Forms.WebBrowser webBrowser1, LearnNet.Common.XmlHelper xmlHelper)
        {
            this.webBrowser1 = webBrowser1;
            this.xmlHelper = xmlHelper;
        }

        public string  Register(RegistEntity regEntity)
        {
            string strReturn = string.Empty;
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/name", regEntity.Name,webBrowser1,xmlHelper))
            {
                strReturn+="名称设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/pwd", regEntity.Pwd,webBrowser1,xmlHelper))
            {
                strReturn+="密码设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/surepwd", regEntity.SurePwd,webBrowser1,xmlHelper))
            {
                strReturn+="确认密码设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/email", regEntity.Email,webBrowser1,xmlHelper))
            {
                strReturn+="电子邮件设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/verificationCode", regEntity.VerificationCode,webBrowser1,xmlHelper))
            {
                strReturn += "验证码设置出错!";
            }
            try
            {
                object obj = webBrowser1.Document.GetElementById("btn_submit").InvokeMember("click");
            }
            catch (Exception e)
            {
                strReturn += e.Message;
            }
            return strReturn;
        }

        public string LoginIn(System.Windows.Forms.WebBrowser webBrowser1, RegistEntity regEntity)
        {
            string strReturn = string.Empty;
            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/login/name", regEntity.Name, webBrowser1, xmlHelper))
            {
                strReturn += "名称设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/login/pwd", regEntity.Pwd, webBrowser1, xmlHelper))
            {
                strReturn += "密码设置出错!";
            }
            try
            {
                object obj = null;
                //webBrowser1.Document.GetElementById("password").SetAttribute("value", regEntity.Pwd);
                //obj = webBrowser1.Document.GetElementById("tk_btn_login").InvokeMember("onmousedown");

                //HtmlElementCollection htmlCollection = webBrowser1.Document.GetElementsByTagName("input");
                //for (int i = 0; i < htmlCollection.Count; i++)
                //{
                //    HtmlElement element = htmlCollection[i];

                //    if (element == "submit")
                //    {
                //        element.InvokeMember("click");
                //    }

                //}
                obj = webBrowser1.Document.GetElementById("TANGRAM__PSP_3__submit").InvokeMember("click");
                
                //obj = webBrowser1.Document.Forms["formLogin"].InvokeMember("submit");
           
                 
            }
            catch (Exception e)
            {
                AppLog.Write("BaiDu登录出错", AppLog.LogMessageType.Error, e);
                strReturn += e.Message;
            }
            return strReturn;
        }

        public string Report(System.Windows.Forms.WebBrowser webBrowser1,ReportContent reportContent)
        {
            string strReturn = string.Empty;
            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/report/title", reportContent.Title, webBrowser1, xmlHelper))
            {
                strReturn += "标题设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/report/content", reportContent.Content, webBrowser1, xmlHelper))
            {
                strReturn += "内容设置出错!";
            }
            try
            {
                webBrowser1.Document.Window.Frames[1].Document.Body.InnerHtml = reportContent.Content;

            }
            catch (Exception ex)
            {
                strReturn += ex.Message;
            }
            try
            {
                Thread.Sleep(1000);
                object obj = null;
                HtmlElementCollection htmlCollection = webBrowser1.Document.GetElementsByTagName("a");
                for (int i = 0; i < htmlCollection.Count; i++)
                {
                    HtmlElement element = htmlCollection[i];

                    if (element.Id == "qPubBtnSubmit")
                    {
                        element.InvokeMember("click");
                        break;
                    }

                }
                

            }
            catch (Exception e)
            {
                AppLog.Write("BaiDu发表出错", AppLog.LogMessageType.Error, e);
                strReturn += e.Message;

            }

            return strReturn;
        }
    }
}
